<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 12/2/14
 * Time: 3:14 PM
 */

namespace Frontend;

use AuthorizedController,
    Jinput,
    Config,
    Lang,
    Validator,
    Sentry,
    Favourite,
    Lyric,
    Lesson,
    Exception,
    HDServices,
    Response,
    View,
    DInput,
    Video;


class ActivitiesController extends AuthorizedController
{
    public function __construct()
    {
        return $this->beforeFilter('auth', array('except' => 'postListComment'));
    }

    /**
     * processing add a favourite content
     * @return number
     */

    public function postAddFavorite()
    {
        $id = Jinput::get('id', '');
        $remove = Jinput::get('remove', '');
        $target = Jinput::get('target', '');
//        $user = Sentry::getUser();
//        if ($id != '' && is_numeric($id) && !is_null($user) && !empty($user->id)) {
//            if ($target == 'lyric') {
//                $lyric = Lyric::find($id);
//                $lyric->favourites()->detach($user->id);
//                if ($remove != true)
//                    $lyric->favourites()->attach($user->id);
//                $lyric->favourites = $lyric->favourites()->count();
//                $lyric->save();
//                return $lyric->favourites;
//            }
//        }
        switch ($target) {
            case 'lesson':
                $target = 'lesson';
                break;
            case 'question':
                $target = 'question';
                break;
            case 'video':
                $target = 'video';
                break;
            case 'answer':
                $target = 'answer';
                break;
            default:
                $target = 'lesson';
                break;
        }
        if ($id != 0 && is_numeric($id)) {
            $params = array('class' => $target, 'id' => $id, 'user_id' => Sentry::getUser()->id, 'remove' => $remove);
            $flag = HDServices::activityServices()->addFavourite($params);
            return $flag;
        }
        return 0;
    }


    /**
     * processing thank a content
     * return number
     */
    public function postAddThank()
    {
        $id = Jinput::get('id', '');
        $remove = Jinput::get('remove', '');
        $target = Jinput::get('target', '');
        $user = Sentry::getUser();
        if ($id != '' && is_numeric($id) && !is_null($user) && !empty($user->id)) {
            if ($target == 'lyric') {
                $lyric = Lyric::find($id);
                $lyric->thanks()->detach($user->id);
                if ($remove != true)
                    $lyric->thanks()->attach($user->id);
                $lyric->thanks = $lyric->thanks()->count();
                $lyric->save();
                return $lyric->thanks;
            }
        }
    }

    public function postAddComment()
    {
        $id = Jinput::get('target_id', 0);
        $type = Jinput::get('type', 'lesson');
        $parent_id = Jinput::get('parent_id', 0);
        $content = strip_tags(Jinput::get('content'));
        $target = 'lesson_id';
        switch ($type) {
            case 'video':
                $target = 'video_id';
                break;
            case 'lesson':
                $target = 'lesson_id';
                break;
            case 'lyric':
                $target = 'lyric_id';
                break;
            default:
                break;
        }
        if (!empty($content)) {
            $data = array('user_id' => Sentry::getUser()->id, $target => $id, 'content' => $content);
            HDServices::activityServices()->addComment($data);
        }
    }

    public function postAddVote()
    {
        $id = Jinput::get('id', 0);
        $type = Jinput::get('target', 'lesson');
        $remove = Jinput::get('remove', false);
        switch ($type) {
            case 'lesson':
                $type = 'lesson';
                break;
            case 'question':
                $type = 'question';
                break;
            case 'video':
                $type = 'video';
                break;
            case 'answer':
                $type = 'answer';
                break;
            default:
                $type = 'lesson';
                break;
        }
        if ($id != 0 && is_numeric($id)) {
            $params = array('class' => $type, 'id' => $id, 'user_id' => Sentry::getUser()->id, 'remove' => $remove);
            $flag = HDServices::activityServices()->addVote($params);
            return $flag;
        }
        return 0;
    }

    public function postListComment()
    {
//        if (Request::ajax()) {
        $target = Jinput::get('target', 'lesson');
        $id = Jinput::get('id', '');
//            if ($id != '' && is_numeric($id)) {
        $comments = HDServices::activityServices()->listComment(array('target' => $target, 'id' => $id));
        return Response::json(View::make('commons/comments/list_comments_ajax', compact('comments'))->render());
//            }
//        }
    }

    public function postJoinCourse()
    {
        if (Request::ajax()) {
            $course = DInput::get('course', '', 'number');
            if ($course != '') {
                if (!is_null($level = \Category::find($course)->levels()->where('level', '=', 1)->first())) {
                    $user = Sentry::getUser();
                    if (is_null($user->inCourses()->where('users_categories.category_id', '=', $level->id)->first()))
                        $user->inCourses()->attach($level->id);
                    return Response::json(array('success'));
                }
            }
        }
        return Response::json(array('false'));
    }
} 